import hashlib
from models.models.RequestResult import RequestResult as Res
from django.views.decorators.csrf import csrf_exempt
from django.http.response import JsonResponse
from models.models import StoreGoods, StoreMenu
from django.shortcuts import render
import json
import math


# 转发到商城商品管理页
def to_store_goods(request):
    fields = StoreGoods.get_fields()
    menus = StoreMenu.objects.filter()
    res_menus = []
    for menu in menus:
        res_menu = dict()
        res_menu['value'] = menu.id
        res_menu['label'] = menu.menu_name
        res_menus.append(res_menu)
    res = {
        "name": "菜单",
        "fields": fields,
        "selectValues": json.dumps({
            "type_id_values": str(res_menus),
        }),
        "getListUrl": "/admin/storeGoods/getList/",
        "getOneUrl": "/admin/storeGoods/getOne/",
        "insertUrl": "/admin/storeGoods/insert/",
        "updateUrl": "/admin/storeGoods/update/",
        "deleteUrl": "/admin/storeGoods/delete/"
    }
    return render(request, "storeAdmin/common/list.html", res)


# 获取商品列表
def get_goods_list(request):
    page_number = request.GET.get("page_number")
    if page_number is None:
        page_number = 1
    else:
        page_number = int(page_number)
    page_size = 10
    limit = (page_number - 1) * page_size
    offset = limit + page_size
    count = 0
    goods = []
    goods_title = request.GET.get("goods_title")
    if not goods_title:
        goods = StoreGoods.objects.filter().order_by("-id")[limit:offset]
        count = StoreGoods.objects.filter().count()
    else:
        goods = StoreGoods.objects.filter(goods_title__contains=goods_title).order_by("-id")[limit:offset]
        count = StoreGoods.objects.filter(goods_title__contains=goods_title).count()
    page_total = math.ceil(count / page_size)
    res_goods = []
    for g in goods:
        res_g = dict()
        res_g["id"] = g.id
        res_g["goods_title"] = g.goods_title
        res_g["goods_keyword"] = g.goods_keyword
        res_g["goods_img"] = g.goods_img
        # res_g["goods_specification"] = g.goods_specification
        if g.type_id is None:
            res_g["type_id"] = "未分类"
        else:
            store_menu = StoreMenu.objects.filter(id=g.type_id).first()
            res_g["type_id"] = store_menu.menu_name
        res_g["is_recommend"] = g.is_recommend
        res_g["is_selection"] = g.is_selection
        res_goods.append(res_g)
    res_date = dict()
    res_date["list"] = res_goods
    res_date["page_number"] = page_number
    res_date["page_total"] = page_total
    return JsonResponse(Res.success(res_date))


# 新增商品
@csrf_exempt
def insert(request):
    goods = StoreGoods()
    goods.goods_title = request.POST.get("goods_title")
    goods.goods_keyword = request.POST.get("goods_keyword")
    goods.goods_img = request.POST.get("goods_img")
    goods.goods_specification = request.POST.get("goods_specification")
    goods.type_id = request.POST.get("type_id")
    goods.goods_wph_price = request.POST.get("goods_wph_price")
    goods.goods_wph_link = request.POST.get("goods_wph_link")
    goods.goods_jd_price = request.POST.get("goods_jd_price")
    goods.goods_jd_link = request.POST.get("goods_jd_link")
    goods.goods_tm_price = request.POST.get("goods_tm_price")
    goods.goods_tm_link = request.POST.get("goods_tm_link")
    goods.goods_tb_price = request.POST.get("goods_tb_price")
    goods.goods_tb_link = request.POST.get("goods_tb_link")
    goods.goods_sn_price = request.POST.get("goods_sn_price")
    goods.goods_sn_link = request.POST.get("goods_sn_link")
    is_recommend = request.POST.get("is_recommend")
    if is_recommend == "true":
        goods.is_recommend = 1
    else:
        goods.is_recommend = 0
    is_selection = request.POST.get("is_selection")
    if is_selection == "true":
        goods.is_selection = 1
    else:
        goods.is_selection = 0
    goods.save()
    return JsonResponse(Res.success(None))


# 获取某一商品
def get_goods_one(request):
    id = request.GET.get('id')
    goods = StoreGoods.objects.filter(id=id).first()
    return JsonResponse(goods.to_dict())


# 更新商品
@csrf_exempt
def update(request):
    goods = StoreGoods.objects.get(id=request.POST.get("id"))
    goods.goods_title = request.POST.get("goods_title")
    goods.goods_keyword = request.POST.get("goods_keyword")
    goods.goods_img = request.POST.get("goods_img")
    goods.goods_specification = request.POST.get("goods_specification")
    goods.type_id = request.POST.get("type_id")
    goods.goods_wph_price = request.POST.get("goods_wph_price")
    goods.goods_wph_link = request.POST.get("goods_wph_link")
    goods.goods_jd_price = request.POST.get("goods_jd_price")
    goods.goods_jd_link = request.POST.get("goods_jd_link")
    goods.goods_tm_price = request.POST.get("goods_tm_price")
    goods.goods_tm_link = request.POST.get("goods_tm_link")
    goods.goods_tb_price = request.POST.get("goods_tb_price")
    goods.goods_tb_link = request.POST.get("goods_tb_link")
    goods.goods_sn_price = request.POST.get("goods_sn_price")
    goods.goods_sn_link = request.POST.get("goods_sn_link")
    is_recommend = request.POST.get("is_recommend")
    if is_recommend == "true":
        goods.is_recommend = 1
    else:
        goods.is_recommend = 0
    is_selection = request.POST.get("is_selection")
    if is_selection == "true":
        goods.is_selection = 1
    else:
        goods.is_selection = 0
    goods.save()
    return JsonResponse(Res.success(None))


# 删除菜单
def delete(request):
    id = request.GET.get("id")
    goods = StoreGoods.objects.filter(id=id).first()
    goods.delete()
    return JsonResponse(Res.success(None))
